﻿Imports System.Data.SqlClient
Imports System.Data.DataView


Public Class FrmRepoPedidos
    Dim stringsql As String
    Protected Const conexion As String = "Data Source=localhost;Initial Catalog=RCBD;Integrated Security=True"
    Private oDataAdapter As SqlDataAdapter
    Dim vcliente As Integer
    Dim vestado As Integer
    Dim fechaini As Date
    Dim fechafin As Date

    Private Sub inhabilitar()
        grupofecha.Enabled = False
    End Sub

    Private Sub llenarcbocli()
        Dim ob As New Clientes
        'Dim tx As String
        Dim origen As String = "SELECT codcliente, nombre, direccion, telefono FROM dbo.Clientes"
        Dim dsclientes As DataTable
        dsclientes = Sql.Llenar(conexion, origen, )
        With Me.cmbcliente
            .DisplayMember = "nombre"
            .ValueMember = "codcliente"
            .DataSource = dsclientes
            .SelectedValue = 0
        End With
    End Sub

    Private Sub comboestado()
        Dim ob As New Estadonotas
        'Dim tx As String
        Dim origen As String = "SELECT codestadonota, descestadonota FROM dbo.ESTADONOTAS"
        Dim dsestadonota As DataTable
        dsestadonota = Sql.Llenar(conexion, origen, )
        With Me.cmbestado
            .DisplayMember = "descestadonota"
            .ValueMember = "codestadonota"
            .DataSource = dsestadonota
            .SelectedValue = 0
        End With
    End Sub

    Private Sub FrmRepoPedidos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        llenarcbocli()
        comboestado()
        vcliente = 0
        vestado = 0
        inhabilitar()

    End Sub
    Private Sub llenarGrid(ByVal stringsql As String)
        'Abrimos la conexion
        cn.Open()
        Me.oDataAdapter = New SqlDataAdapter(stringsql, cn)
        'Crear el Datasets
        Dim oDataSet As New DataSet
        'Llenamos el Dataset
        Me.oDataAdapter.Fill(oDataSet, "VistaPedidosPend")
        'Cerramos la conexion
        cn.Close()
        dgvpedidos.DataSource = oDataSet.Tables("VistaPedidosPend")
        dgvpedidos.Refresh()
    End Sub

    Private Sub btncerrar_Click(sender As System.Object, e As System.EventArgs) Handles btncerrar.Click
        Me.Close()
        Me.Finalize()


    End Sub

    Private Sub btnbuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnbuscar.Click
        If vcliente <> 0 And vestado <> 0 And chkfecharec.CheckState = 1 Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE codcliente= '" & vcliente & "' AND CODESTADO='" & vestado & "' AND FECHA_PEDIDO BETWEEN '" & fechaini & "' AND '" & fechafin & "'"
        End If
        If vcliente <> 0 And vestado <> 0 And chkfecharec.CheckState = CheckState.Unchecked And chkfechavec.CheckState = CheckState.Unchecked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE codcliente= '" & vcliente & "' AND CODESTADO='" & vestado & "'"
        End If
        If vcliente <> 0 And vestado = 0 And chkfecharec.CheckState = CheckState.Unchecked And chkfechavec.CheckState = CheckState.Unchecked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE codcliente= '" & vcliente & "'"
        End If
        If vcliente = 0 And vestado <> 0 And chkfecharec.CheckState = CheckState.Unchecked And chkfechavec.CheckState = CheckState.Unchecked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE CODESTADO='" & vestado & "' "
        End If
        If vcliente <> 0 And vestado = 0 And chkfecharec.CheckState = CheckState.Checked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE codcliente= '" & vcliente & "' AND FECHA_PEDIDO BETWEEN '" & fechaini & "' AND '" & fechafin & "'"
        End If
        If vcliente <> 0 And vestado = 0 And chkfechavec.CheckState = CheckState.Checked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE codcliente= '" & vcliente & "' AND FECHA_ENTREGA BETWEEN '" & fechaini & "' AND '" & fechafin & "'"
        End If
        If vcliente = 0 And vestado = 0 And chkfechavec.CheckState = CheckState.Checked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE FECHA_ENTREGA BETWEEN '" & fechaini & "' AND '" & fechafin & "'"
        End If
        If vcliente = 0 And vestado = 0 And chkfecharec.CheckState = CheckState.Checked Then
            stringsql = "SELECT NRO_PEDIDO, CLIENTE, FECHA_PEDIDO, FECHA_ENTREGA, ESTADO, ORDEN_COMP FROM VistaPedidosPend WHERE FECHA_PEDIDO BETWEEN '" & fechaini & "' AND '" & fechafin & "'"
        End If

        llenarGrid(stringsql)


    End Sub

    Private Sub cmbcliente_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbcliente.SelectedIndexChanged
        vcliente = cmbcliente.SelectedValue
    End Sub

    Private Sub cmbestado_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbestado.SelectedIndexChanged
        vestado = CInt(cmbestado.SelectedValue)

    End Sub

    Private Sub chkfecharec_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkfecharec.CheckedChanged
        If chkfecharec.CheckState = CheckState.Checked Then
            grupofecha.Enabled = True
            chkfechavec.CheckState = CheckState.Unchecked
        Else
            grupofecha.Enabled = False

        End If
        

    End Sub

    Private Sub chkfechavec_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkfechavec.CheckedChanged
        If chkfechavec.CheckState = CheckState.Checked Then
            grupofecha.Enabled = True
            chkfecharec.CheckState = CheckState.Unchecked
        Else
            grupofecha.Enabled = False

        End If

    End Sub

    Private Sub chkfechavec_CheckStateChanged(sender As Object, e As System.EventArgs) Handles chkfechavec.CheckStateChanged

    End Sub

    Private Sub dtpinicio_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtpinicio.ValueChanged
        fechaini = CDate(dtpinicio.Value).ToString("yyyy/MM/dd")
    End Sub


    Private Sub dtpfin_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtpfin.ValueChanged
        fechafin = CDate(dtpfin.Value).ToString("yyyy/MM/dd")
    End Sub

    
    Private Sub btnlimpiar_Click(sender As System.Object, e As System.EventArgs) Handles btnlimpiar.Click
        cmbcliente.SelectedValue = 0
        cmbestado.SelectedValue = 0
    End Sub
End Class